﻿using System.Drawing;
using DevExpress.Utils.Drawing;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;

namespace ContractManager.Common.Service
{
    /// <summary>
    /// Select row in view. 
    /// </summary>
    /// <remarks>
    /// Golovkov Alex, 07.10.2010. 
    /// </remarks>
    public class SelectRowInView
    {
        #region Constants and Fields

        

        /// <summary>
        /// The column view.
        /// </summary>
        private readonly ColumnView view;

        /// <summary>
        /// The edit.
        /// </summary>
        private RepositoryItemCheckEdit edit = new RepositoryItemCheckEdit();

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SelectRowInView"/> class. 
        /// Ctor. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        /// <param name="view">
        /// column view 
        /// </param>
        public SelectRowInView(ColumnView view)
        {
            this.view = view;
            
        }

        #endregion

        #region Properties

        

        

        #endregion

        #region Public Methods

        /// <summary>
        /// Î÷èñòêà êîëëåêöèè âûäåëåííûõ. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        public void ClearSelection()
        {
            
            this.MyInvalidate();
        }

        /// <summary>
        /// Draw check box. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        /// <param name="g">
        /// The. 
        /// </param>
        /// <param name="r">
        /// The. 
        /// </param>
        /// <param name="checked">
        /// The checked.
        /// </param>
        public void DrawCheckBox(Graphics g, Rectangle r, bool @checked)
        {
            var info = this.edit.CreateViewInfo() as CheckEditViewInfo;
            var painter = this.edit.CreatePainter() as CheckEditPainter;
            if (info == null)
            {
                return;
            }

            info.EditValue = @checked;
            info.Bounds = r;
            info.CalcViewInfo(g);
            var args = new ControlGraphicsInfoArgs(info, new GraphicsCache(g), r);

            if (painter != null)
            {
                painter.Draw(args);
            }

            args.Cache.Dispose();
        }

        

        /// <summary>
        /// Query if 'rowHandle' is group row selected. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        /// <param name="rowHandle">
        /// Handle of the row. 
        /// </param>
        /// <returns>
        /// true if group row selected, false if not. 
        /// </returns>
        public bool IsGroupRowSelected(int rowHandle)
        {
            var localView = this.view as GridView;

            if (localView != null)
            {
                for (int i = 0; i < localView.GetChildRowCount(rowHandle); i++)
                {
                    int row = localView.GetChildRowHandle(rowHandle, i);
                    if (localView.IsGroupRow(row))
                    {
                        if (!this.IsGroupRowSelected(row))
                        {
                            return false;
                        }
                    }
                   
                }
            }

            return true;
        }

        

        /// <summary>
        /// Select all. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        public void SelectAll()
        {
            
            this.MyInvalidate();
        }

        /// <summary>
        /// Select group. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        /// <param name="rowHandle">
        /// Handle of the row. 
        /// </param>
        /// <param name="select">
        /// true to select, false to deselect. 
        /// </param>
        public void SelectGroup(int rowHandle, bool select)
        {
            if (this.IsGroupRowSelected(rowHandle) && select)
            {
                return;
            }

            var localView = this.view as GridView;

            if (localView != null)
            {
                for (int i = 0; i < localView.GetChildRowCount(rowHandle); i++)
                {
                    int childRowHandle = localView.GetChildRowHandle(rowHandle, i);
                    if (localView.IsGroupRow(childRowHandle))
                    {
                        this.SelectGroup(childRowHandle, select);
                    }
                    else
                    {
                        this.SelectRow(childRowHandle, select, false);
                    }
                }
            }

            this.MyInvalidate();
        }

        /// <summary>
        /// Âûäåëåíèå ñòðîêè. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        /// <param name="rowHandle">
        /// Handle ñòðîêè. 
        /// </param>
        /// <param name="select">
        /// ïðèçíàê - âûäåëåíà/íåâûäåëåíà. 
        /// </param>
        public void SelectRow(int rowHandle, bool select)
        {
            this.SelectRow(rowHandle, select, true);
        }

        #endregion

        #region Methods

        /// <summary>
        /// My invalidate. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        private void MyInvalidate()
        {
            this.view.BeginUpdate();
            this.view.EndUpdate();
        }

        /// <summary>
        /// Âûäåëåíèå ñòðîêè. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 07.10.2010. 
        /// </remarks>
        /// <param name="rowHandle">
        /// Handle ñòðîêè. 
        /// </param>
        /// <param name="select">
        /// ïðèçíàê - âûäåëåíà/íåâûäåëåíà. 
        /// </param>
        /// <param name="invalidate">
        /// true to invalidate. 
        /// </param>
        private void SelectRow(int rowHandle, bool select, bool invalidate)
        {
            

            if (invalidate)
            {
                this.MyInvalidate();
            }
        }

        #endregion
    }
}
